package com.dub.firstapp;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dub.iterface.ISessionHandle;
import com.dub.model.SecRpcModel;
import com.dub.utils.Session;
import com.dub.utils.Validate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements ISessionHandle {
    EditText login_phone;
    EditText login_pwd;
    List<String> commands=new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Session.GetSession();
        //添加处理的命令
        commands.add("1-1");

        login_phone=(EditText)findViewById(R.id.edt_loginPhone);
        login_pwd=(EditText)findViewById(R.id.edt_loginPwd);
        Button btn_login=(Button)findViewById(R.id.btn_login);

        login_phone.setOnFocusChangeListener(new View.OnFocusChangeListener(){
            public void onFocusChange(View v,boolean hasFocus){
                if (hasFocus){
                    return;
                }
                valiPhone();
            }
        });

        login_pwd.setOnFocusChangeListener(new View.OnFocusChangeListener(){
            public void onFocusChange(View v, boolean hasFocus){
                if (hasFocus){
                    return;
                }
                valiPwd();
            }
        });

        btn_login.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view) {
                //验证用户的输入是否合法
                if (valiPhone()&&valiPwd()){
//                if (false){
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            SecRpcModel.ModelSecRpcAgentReqFind mdl=SecRpcModel.ModelSecRpcAgentReqFind.newBuilder()
                                    .setLoginName(login_phone.getText().toString().trim())
                                    .setLoginPwd(login_pwd.getText().toString())
                                    .setReqType(3)
                                    .build();

                            //添加处理函数与发送命令
                            Session.GetSession().AddHand("MainActivity",MainActivity.this);
                            byte[] data=mdl.toByteArray();
                            Session.GetSession().Send((short)1,(short)1,data);
                        }
                    }).start();
                }
            }
        });
    }

    //验证手机号
    private boolean valiPhone(){
        String phone_num=login_phone.getText().toString().trim();
        if (!Validate.Mphone(phone_num)){
            Toast.makeText(MainActivity.this,"手机号格式错误",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
    //验证密码
    private boolean valiPwd(){
        String pwd=login_pwd.getText().toString();
        if (!Validate.Pwd(pwd)){
            Toast.makeText(MainActivity.this,"密码必须是6到16位任意字符",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public List<String> GetHandCmd() {
        return commands;
    }

    @Override
    public void HandAction(String cmd, byte[] data) {
        try{
            //因为只处理1-1的命令所以可以直接用session里的对象
            if (Session.GetSession().GetAgent().getErr()==0&&Session.GetSession().GetAgent().getModelSecRpcAgent().getAgentId()>0){
                //启动页面信息
                Intent intent=new Intent(MainActivity.this,ShareActivity.class);
                startActivity(intent);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
